package loss;

/**
 * LogLinear Loss Function
 * @author tigerzhong
 *
 */
public class LogLinear implements LossFun{

    @Override
    public double[] getGradient(double[] parameters, double[] instance, double target) {
	int len = instance.length;
	double dev[] = new double[len];
	for(int i=0;i<len;i++){
	    double lambda = (parameters[i] * instance[i]);
	    dev[i] = target / lambda * instance[i];
	}
	return dev;
    }

    @Override
    public double getLoss(double[] parameters, double[] instance, double target) {
	int len = instance.length;
	double lambda = 0.0;
	for(int i=0;i<len;i++){
	    lambda += parameters[i] * instance[i];
	}
	return (target * Math.log(lambda) - lambda);
    }

}
